package com.dingwei.superiordiary.adapter.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bobomee.android.mentions.edit.util.FormatRangeManager;
import com.bobomee.android.mentions.text.MentionTextView;
import com.bumptech.glide.Glide;
import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.app.MyApplication;
import com.dingwei.superiordiary.bean.CommentBean;
import com.dingwei.superiordiary.parser.Parser;
import com.dingwei.superiordiary.ui.activity.MoreReplyActivity;
import com.dingwei.superiordiary.ui.activity.PhotoDetailsActivity;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.weight.MyGridView;

import java.io.Serializable;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by lihao on 2018/1/8.
 */

public class FindDetailsEvaluationListAdapter extends RecyclerArrayAdapter<CommentBean.DataBean.ListBean> {

    private ReplyInterface replyInterface;
    private FormatRangeManager mRangeManager;
    private Parser mTagParser = new Parser();
    public void setReplyInterface(ReplyInterface replyInterface) {
        this.replyInterface = replyInterface;
    }

    public FindDetailsEvaluationListAdapter(Context context) {
        super(context);
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new FindDetailsEvaluationListHolder(parent);
    }

    class FindDetailsEvaluationListHolder extends BaseViewHolder<CommentBean.DataBean.ListBean> {


        @Bind(R.id.img_head)
        ImageView imgHead;
        @Bind(R.id.tv_nick_name)
        TextView tvNickName;
        @Bind(R.id.tv_level)
        TextView tvLevel;
        @Bind(R.id.tv_time)
        TextView tvTime;
        @Bind(R.id.tv_content)
        MentionTextView tvContent;
        @Bind(R.id.gridview)
        MyGridView gridview;
        @Bind(R.id.m_recyclerView)
        RecyclerView mRecyclerView;
        @Bind(R.id.tv_more)
        TextView tvMore;
        @Bind(R.id.sub_layout)
        LinearLayout subLayout;
        @Bind(R.id.tv_evaluation_num)
        TextView tvEvaluationNum;
        @Bind(R.id.img_like)
        ImageView imgLike;
        @Bind(R.id.tv_like_num)
        TextView tvLikeNum;
        @Bind(R.id.like_layout)
        LinearLayout likeLayout;
        @Bind(R.id.ic_tread)
        ImageView icTread;
        @Bind(R.id.tv_tread_num)
        TextView tvTreadNum;
        @Bind(R.id.tread_layout)
        LinearLayout treadLayout;
        @Bind(R.id.tv_floor)
        TextView tvFloor;

        SecondaryCommentAdaper mAdapter = new SecondaryCommentAdaper(BaseActivity.context);


        public FindDetailsEvaluationListHolder(ViewGroup parent) {
            super(parent, R.layout.item_details_evaluation_list);
            ButterKnife.bind(this, itemView);

            LinearLayoutManager layoutManager = new LinearLayoutManager(BaseActivity.context);
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            mRecyclerView.setLayoutManager(layoutManager);
            mRecyclerView.setNestedScrollingEnabled(false);
            mRecyclerView.setAdapter(mAdapter);
            mRangeManager = new FormatRangeManager();
            tvContent.setMovementMethod(new LinkMovementMethod());
            tvContent.setParserConverter(mTagParser);
        }


        @Override
        public void setData(final CommentBean.DataBean.ListBean data) {
            super.setData(data);
            Glide.with(BaseActivity.context)
                    .load(data.portrait)
                    .apply(MyApplication.headOptions)
                    .into(imgHead);
            tvNickName.setText(data.nickname);
            tvTime.setText(data.addtime);
            String s = data.content.replaceAll("'>", "'>@");
            tvContent.setText(s);
            tvLevel.setText("LV."+data.level);
            if (data.is_liked == 1) {
                imgLike.setImageResource(R.mipmap.ic_liked);
            } else {
                imgLike.setImageResource(R.mipmap.ic_like);
            }
            tvLikeNum.setText(data.likes);
            tvTreadNum.setText(data.trample);
            tvEvaluationNum.setText(data.comment_total+"");

            if (!TextUtils.isEmpty(data.floor)){
                tvFloor.setText(data.floor+"楼");
                tvFloor.setVisibility(View.VISIBLE);
            }else {
                tvFloor.setVisibility(View.GONE);
            }
            if (data.sub != null && data.sub.size() > 0) {
                mAdapter.clear();
                mAdapter.addAll(data.sub);
                subLayout.setVisibility(View.VISIBLE);
                if (data.more == 1) {
                    tvMore.setVisibility(View.GONE);
                } else {
                    tvMore.setVisibility(View.VISIBLE);
                }

                mAdapter.setXXInterface(new SecondaryCommentAdaper.XXInterface() {
                    @Override
                    public void like(int position) {
                        if (replyInterface != null) {
                            replyInterface.Sublikes(getDataPosition(),position);
                        }
                    }

                    @Override
                    public void uers(int position) {
                        CommentBean.DataBean.ListBean.SubBean bean = mAdapter.getItem(position);
                        if (replyInterface != null) {
                            replyInterface.subUser(bean.id);
                        }
                    }
                });
                mAdapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(int i) {
                        Intent intent = new Intent(BaseActivity.context, MoreReplyActivity.class);
                        intent.putExtra("id", data.id);
                        BaseActivity.context.startActivity(intent);
                    }
                });

            } else {
                subLayout.setVisibility(View.GONE);
            }
            likeLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (replyInterface != null) {
                        replyInterface.likes(getDataPosition());
                    }
                }
            });
            tvMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (replyInterface != null) {
                        replyInterface.more(getDataPosition());
                    }
                }
            });
            FindDetailsPicAdapter adapter = new FindDetailsPicAdapter(BaseActivity.context, data.image);
            gridview.setAdapter(adapter);
            gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Intent intent = new Intent();
                    intent.setClass(BaseActivity.context, PhotoDetailsActivity.class);
                    intent.putExtra("position", i);
                    intent.putExtra("img", (Serializable) data.image);
                    BaseActivity.context.startActivity(intent);
                }
            });

            treadLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (replyInterface != null) {
                        replyInterface.cai(getDataPosition());
                    }
                }
            });

            imgHead.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    replyInterface.subUser(data.consumer_id);
                }
            });
            tvContent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(BaseActivity.context, MoreReplyActivity.class);
                    intent.putExtra("id", data.id);
                    BaseActivity.context.startActivity(intent);
                }
            });


        }
    }


    public interface ReplyInterface {

        void likes(int position);
        void Sublikes(int dataPosition, int position);

        void cai(int position);
        void subUser(String user_id);

        void more(int position);
    }
}
